package com.thinkit.bigdata.web.service.impl.sys;

import com.google.common.base.Strings;
import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.core.util.DateFormatUtil;
import com.thinkit.bigdata.web.dao.sys.SysApprovalDataMapper;
import com.thinkit.bigdata.web.model.result.Constants;
import com.thinkit.bigdata.web.model.result.HtmlResult;
import com.thinkit.bigdata.web.model.sec.User;
import com.thinkit.bigdata.web.model.sys.SysApprovalData;
import com.thinkit.bigdata.web.model.sys.SysApprovalDataDto;
import com.thinkit.bigdata.web.model.sys.SysApprovalDataExample;
import com.thinkit.bigdata.web.service.sys.SysApprovalDataService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class SysApprovalDataServiceImpl implements SysApprovalDataService {

    @Resource
    private SysApprovalDataMapper dataMapper;

    @Override
    public HtmlResult deleteByExample(SysApprovalDataExample example) {
        return null;
    }

    @Override
    public HtmlResult deleteByPrimaryKey(String datacode) {
        for (String code : datacode.split(",")) {
            if (dataMapper.deleteByPrimaryKey(code) == 0) {
                return HtmlResult.failure("删除失败！");
            }
        }
        return HtmlResult.success("删除成功！");
    }

    @Override
    public HtmlResult insertSelective(SysApprovalData record) {
        record.setInputtime(new Date());
        // 获取当前登录用户信息
        User userInfo = (User) SecurityUtils.getSubject().getSession().getAttribute(Constants.SESSION_USER_INFO);
        record.setCreateuser(userInfo.getId());
        if (dataMapper.insertSelective(record) == 0) {
            return HtmlResult.failure("添加失败！");
        }
        return HtmlResult.success("添加成功！");
    }

    @Override
    public HtmlResult selectByExample() {
        SysApprovalDataExample example = new SysApprovalDataExample();
        example.createCriteria().andDatacodeIsNotNull();
        return HtmlResult.success(dataMapper.selectByExample(example));
    }

    @Override
    public HtmlResult selectByExample(SysApprovalDataDto record) {
        SysApprovalDataExample example = new SysApprovalDataExample();
        SysApprovalDataExample.Criteria criteria = example.createCriteria();
        if (!Strings.isNullOrEmpty(record.getDatacode())) {
            criteria.andDatacodeLike("%" + record.getDatacode() + "%");
        }
        if (!Strings.isNullOrEmpty(record.getDataname())) {
            criteria.andDatanameLike("%" + record.getDataname() + "%");
        }
        if (record.getSkipflag() != null) {
            criteria.andSkipflagEqualTo(record.getSkipflag());
        }
        if (!Strings.isNullOrEmpty(record.getNote())) {
            criteria.andNoteLike("%" + record.getNote() + "%");
        }
        if (record.getCreateuser() != null) {
            criteria.andCreateuserEqualTo(record.getCreateuser());
        }
        if (!Strings.isNullOrEmpty(record.getInputtimebegin())) {
            criteria.andInputtimeGreaterThanOrEqualTo(DateFormatUtil.stringToDate(record.getInputtimebegin(), DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(record.getInputtimeend())) {
            criteria.andInputtimeLessThanOrEqualTo(DateFormatUtil.stringToDate(record.getInputtimeend(), DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(record.getLastupdatetimebegin())) {
            criteria.andLastupdatetimeGreaterThanOrEqualTo(DateFormatUtil.stringToDate(record.getLastupdatetimebegin(), DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        if (!Strings.isNullOrEmpty(record.getLastupdatetimeend())) {
            criteria.andLastupdatetimeLessThanOrEqualTo(DateFormatUtil.stringToDate(record.getLastupdatetimeend(), DateFormatUtil.yyyy_MM_dd_HH_mm_ss));
        }
        Page<SysApprovalData> dataPage = new Page<>(record.getPageNum() + 1, record.getPageSize());
        return HtmlResult.success(dataMapper.selectByExample(dataPage, example), dataPage.getTotalCount());
    }

    @Override
    public HtmlResult selectByPrimaryKey(String datacode) {
        return HtmlResult.success(dataMapper.selectByPrimaryKey(datacode));
    }

    @Override
    public HtmlResult updateByPrimaryKey(SysApprovalData record) {
        // 获取当前登录用户信息
        User userInfo = (User) SecurityUtils.getSubject().getSession().getAttribute(Constants.SESSION_USER_INFO);
        record.setLastupdateuser(userInfo.getId());
        record.setLastupdatetime(new Date());
        if (dataMapper.updateByPrimaryKey(record) == 0) {
            return HtmlResult.failure("修改失败！");
        }
        return HtmlResult.success("修改成功！");
    }

    public HtmlResult uniquenessCode(String code) {
        if (dataMapper.selectByPrimaryKey(code) == null) {
            return HtmlResult.success("数据标识不存在");
        }
        return HtmlResult.failure("数据标识存在");
    }

    public HtmlResult uniquenessName(String code, String name) {
        //查询业务层记录
        SysApprovalDataExample example = new SysApprovalDataExample();
        example.createCriteria().andDatanameEqualTo(name);
        List<SysApprovalData> datas = dataMapper.selectByExample(example);
        if (!datas.isEmpty()) {
            if (!Strings.isNullOrEmpty(code)) {
                SysApprovalDataExample example1 = new SysApprovalDataExample();
                example1.createCriteria().andDatacodeEqualTo(code).andDatanameEqualTo(name);
                List<SysApprovalData> keywordss = dataMapper.selectByExample(example1);
                if (!keywordss.isEmpty()) {
                    return HtmlResult.success("数据名称不存在！");
                }
            }
            return HtmlResult.failure("【code = " + code + "、name = " + name + "】数据名称存在！");
        }
        return HtmlResult.success("【code = " + code + "、name = " + name + "】数据名称不存在！");
    }
}
